<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Toplist extends Controller
{
    public function index()
    {
       return  $this->fetch('toplist/toplist');
    }

//    显示排行榜
    public function list_select()
    {

        $data = Db::name('to_top')->field(['m_id', 'id','m_time','m_count'])->select();
        $this->assign('list',$data);
        return $this->fetch('toplist/toplist');

    }


    public function add(){
        return view('toplist/toplist_add');
    }
    public function list_add(){
        if (request()->isPost()) {
            $data = [

                'm_id' => input('m_id'),
                'id' => input('id'),
                'm_time' => input('m_time'),
                'm_count' => input('m_count')
            ];
//            $validate = \think\Loader::validate('Links');
//            if (!$validate->scene('add')->check($data)) {
//                $this->error($validate->getError());
//                die;
//            };


            if (db('to_top')->insert($data)) {
                return $this->success('添加排行榜成功', ('admin/toplist/index'));
            } else {
                return $this->error('添加排行榜失败');
            }
            return;
        }
    }


    public function edit()
    {


        $id = input('id');
      $data = db('to_top')->find($id);


        $m_id = $data['m_id'];
        $m_time = $data['m_time'];
        $m_count = $data['m_count'];


        return $this->fetch('toplist/topedit', [
            'id' => $id,
            'm_id' => $m_id,
            'm_time' => $m_time,
            'm_count' => $m_count

        ]);


    }

    public function edits(){
        $id = input('id');

        $data = db('to_top')->find($id);

        if (request()->isPost()) {
            $data = [
                'id' => $id,
                'm_id' => input('m_id'),
                'm_time' => input('m_time'),
                'm_count' => input('m_count')
            ];


//            $validate = \think\Loader::validate('Links');
//            if (!$validate->check($data)) {
//                $this->error($validate->getError());
//                die;
//            }
            if (db('to_top')->update($data)) {
                $this->success('修改排行榜成功',('admin/toplist/index'));  //??

            } else {
                $this->error('修改排行榜失败');
            }
            return;
        }
    }










//删除排行榜信息

    public function del()
    {
        $id =input('id');

        if (db('to_top')->delete($id)) {

            $this->success('删除排行榜成功', 'admin/Toplist/index');
        } else {
            $this->error('删除排行榜失败');
        }
        return;



    }
}
